package com.fau.cm.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCallHealthSymptom;
import com.fau.cm.service.ICallHealthService;
import com.fau.cm.util.ValidatorUtil;

@Service
@Transactional(readOnly = true)
public class CallHealthServiceImpl extends BaseService implements ICallHealthService {

	@SuppressWarnings("all")
	@Override
	public List<MedicalCallHealthSymptom> findCallHealth(MedicalCallHealthSymptom healthSymptom) {
		
		StringBuilder hql = new StringBuilder("from MedicalCallHealthSymptom m where m.hospitalId=? ");
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(healthSymptom.getHospitalId());
		if(StringUtils.isNotBlank(healthSymptom.getName())) {
			hql.append("and m.name like ? ");
			objectList.add("%"+healthSymptom.getName()+"%");
		}
		return this.iBaseDao.getBySql(hql.toString(), null, objectList.toArray());
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean saveCallHealth(MedicalCallHealthSymptom healthSymptom,MedicalAdmin admin) {
		healthSymptom.setHospitalId(admin.getEnterpriseId());
		healthSymptom.setUserId(admin.getId());
		Set<ConstraintViolation<Object>> errorInfo = ValidatorUtil.validateObj(healthSymptom);
		if(errorInfo.size()>0){
			return false;
		}else{
			return this.iBaseDao.saveObject(healthSymptom);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delCallHealth(MedicalCallHealthSymptom healthSymptom) {
		this.iBaseDao.deleteObject(healthSymptom);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallHealth(MedicalCallHealthSymptom healthSymptom) {
		this.iBaseDao.saveObject(healthSymptom);
	}

	@Override
	public boolean judgeHealth(MedicalCallHealthSymptom healthSymptom) {
		MedicalCallHealthSymptom mchs =(MedicalCallHealthSymptom)this.iBaseDao.getObjectByHql("from MedicalCallHealthSymptom m where m.pid = ? and m.id=? ","0",healthSymptom.getId());
		if(null!=mchs){
			return false; 
		}else{
			return true;
		}
	}
}
